clear; close all; addpath('../utilities');

load Figure_2_out pmf drop pdf hh


% Barro/Ursua crises distribution
BU_data   = xlsread('../data/barro_ursua_tables.xlsx','C1:E114');
N         = size(BU_data,1);
magnitude = BU_data(:,3);
duration  = BU_data(:,1)-BU_data(:,2);
[nd,xd] = hist(duration,1:10);
[nm,xm] = hist(-100*magnitude);
nd      = nd./sum(nd);
nm      = nm./sum(nm)/(xm(2)-xm(1));



ff = figure('Position', [100, 100, 800, 710]);
   set(ff,'Units','Inches');
   pos = get(ff,'Position');
   set(ff,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
subplot = @(m,n,p)subtightplot(m, n, p, [0.13 0.13], [0.08 0.01], [0.09 0.01]);
   
subplot(2,4,[1,2]);
bar(xd,nd,'FaceColor',[0.8,0.8,0.8]); hold on;
plot((hh)/12,12*pmf,'r','LineWidth',1.5);
set(gca,'FontSize',13)
xlabel('Duration (years)','FontSize',15);
ylabel('PDF','FontSize',15);
grid on
ax=axis; axis([0,15,0,ax(4)])
xticks(0:5:15)
ytickformat('%.2f')
box off;

subplot(2,4,[3,4]);
bar(xm,nm,'FaceColor',[0.8,0.8,0.8]); hold on;
plot(sort(-drop),flip(pdf),'r','LineWidth',1.5);
set(gca,'FontSize',13)
xlabel('Cumulative consumption drop (%)','FontSize',15);
ylabel('PDF','FontSize',15);
grid on
ax=axis; axis([-80,5,0,ax(4)])
yticks(0:0.01:0.07)
ytickformat('%.2f')
box off;

subplot(2,4,[6,7]);
unique_durations = unique(duration);
avg_magnitude = NaN(size(unique_durations));
for i=1:length(unique_durations)
    avg_magnitude(i) = mean(magnitude(duration==unique_durations(i)));
end
durations = 1:1/12:9;
plot(durations,100*(exp(-durations*0.0544-0.0244^2*durations/2)-1),'r-','LineWidth',1.5); 
hold on;
plot(unique_durations,-100*avg_magnitude,'ko','LineWidth',2,'MarkerSize',6)
xlim([0,10])
grid on;
set(gca,'FontSize',13)
ylabel({'Mean cumulative','consumption drop (%)'},'FontSize',15);
xlabel('Duration (years)','FontSize',15);

print(gcf,'figure_2','-dpdf','-r0'); 

